package com.aooci.ordercat.desktop.view;

import java.io.IOException;
import java.net.URL;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import javafx.print.PrinterJob;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import com.aooci.ordercat.desktop.OrdercatMain;
import com.aooci.ordercat.desktop.SystemConstant;
import com.aooci.ordercat.domain.Ordercat;
import com.aooci.ordercat.enums.OrderStatus;
import com.aooci.ordercat.vo.ItemVo;
import com.aooci.ordercat.vo.OperatedOrderVo;
import com.aooci.ordercat.vo.OrdercatVo;
import com.aooci.ordercat.vo.RecetteVo;
import com.aooci.ordercat.vo.TakeawayVo;

@SuppressWarnings("restriction")
public class AccountingController implements Initializable {

	private OrdercatMain ordercatMain;
	
	// Item table
    @FXML private TableView<OrdercatVo> orderTable;
    @FXML private TableColumn<OrdercatVo, String> idColumn;
    @FXML private TableColumn<OrdercatVo, String> typeColumn;
    @FXML private TableColumn<OrdercatVo, String> statusColumn;
    @FXML private TableColumn<OrdercatVo, String> creationColumn;
    @FXML private TableColumn<OrdercatVo, String> totalColumn;

    private OrdercatVo selectedOrdercatVo;

    @FXML private DatePicker date;
    @FXML private ChoiceBox<OrderStatus> status = new ChoiceBox<OrderStatus>();
    @FXML private ChoiceBox<Integer> divideTime = new ChoiceBox<Integer>();
	@FXML private Button loadOrdersButton;
	@FXML private Button closeButton;

	@FXML
	public void initialize(URL arg0, ResourceBundle arg1) {
		
    	// Initialize the item table.
		idColumn.setCellValueFactory(cellData -> cellData.getValue().idProperty());
		typeColumn.setCellValueFactory(cellData -> cellData.getValue().typeProperty());
		statusColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());
		creationColumn.setCellValueFactory(cellData -> cellData.getValue().creationProperty());
		totalColumn.setCellValueFactory(cellData -> cellData.getValue().totalProperty());
		//orderedItemAmountColumn.setCellValueFactory(cellData -> cellData.getValue().amountProperty());
		orderTable.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> setSelectedOrdercatVo(newValue));
		
		
		this.closeButton.setOnAction((event) -> {
			((Stage) this.closeButton.getScene().getWindow()).close();
		});
		
		//status = new ChoiceBox<String>(FXCollections.observableArrayList("Paid", "Occupied", "Discarded"));
		status.getItems().addAll(OrderStatus.PAID, OrderStatus.SERVICING, OrderStatus.DISCARDED);
		status.getSelectionModel().selectFirst();
		
		divideTime.getItems().addAll(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23);
		divideTime.getSelectionModel().selectFirst();
	}


	public void init(OrdercatMain ordercatMain) {
		this.ordercatMain = ordercatMain;
	}

	@FXML
    public void loadOrdercats(){
    	//this.orderTable.getItems().clear();
    	this.orderTable.setItems(FXCollections.observableArrayList(this.ordercatMain.getAccountingService().loadOrdercatsBySpecifiedDate(Date.from(this.date.getValue().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()), this.status.getValue(), this.divideTime.getValue().intValue())));
    	
    }
	
	public void deleteOrdercat(){
		this.orderTable.getItems().remove(this.selectedOrdercatVo);
		this.ordercatMain.getAccountingService().deleteOrdercat(Integer.parseInt(this.selectedOrdercatVo.getId()));
	}
	
	@FXML
	public void exportRecette(){
//		//Node node = new Circle(100, 200, 200);
//		PrinterJob job = PrinterJob.createPrinterJob();
//		if (job != null) {
//		   boolean success = job.printPage(orderTable);
//		   if (success) {
//		       job.endJob();
//		   }
//		}
		List<RecetteVo> recetteList = this.ordercatMain.getAccountingService().loadRecettesBySpecifiedDate(Date.from(this.date.getValue().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()), divideTime.getValue());
		try {
			FXMLLoader loader = new FXMLLoader();
			loader.setLocation(OrdercatMain.class.getResource(SystemConstant.RECETTE_FXML));
			VBox page = (VBox) loader.load();

			Stage dialogStage = new Stage();
			dialogStage.initStyle(StageStyle.UNDECORATED);
			dialogStage.setTitle(SystemConstant.RECETTE_TITLE);
			dialogStage.initModality(Modality.APPLICATION_MODAL);
			dialogStage.initOwner(this.ordercatMain.getPrimaryStage());
			dialogStage.setResizable(true);
			dialogStage.setFocused(true);
			
			Scene scene = new Scene(page);
			dialogStage.setScene(scene);
			scene.getStylesheets().add("css/desktop.css");
			RecetteController controller = loader.getController();
			controller.init(ordercatMain, page, recetteList);
			//controller.init(ordercatMain, dialogStage, operatedOrderVo, takeawayTable);
			dialogStage.showAndWait();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public void setSelectedOrdercatVo(OrdercatVo selectedOrdercatVo) {
		this.selectedOrdercatVo = selectedOrdercatVo;
	}
	
	
}
